package com.mybatis.mapper;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.mybatis.entity.Blog;
import com.mybatis.entity.BlogUser;

public class BlogMapperTester {

	private static SqlSessionFactory sessionFactory = null;
	private static SqlSession session = null;
	private static final Log log = LogFactory.getLog(BlogMapperTester.class);


	@Before
	public void init() throws IOException {

		String configResource = "Configuration.xml";

		Reader reader = Resources.getResourceAsReader(configResource);

		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

		sessionFactory = builder.build(reader);

		// 默認事務是不自動提交
		// session = sessionFactory.openSession();

		// 設置自動提交事務
		session = sessionFactory.openSession(true);

		// 批量處理
		// session = sessionFactory.openSession(ExecutorType.BATCH, true);
	}

	@After
	public void afterMethodInvoke() {

		session.close();

	}

	@Test
	public void testFindBlogById() throws IOException {

		BlogMapper blogMapper = session.getMapper(BlogMapper.class);

		Blog blog = blogMapper.findBlogById(1);

		System.out.println(blog);

	}

	@Test
	public void testselectAllBlog() throws IOException {

		BlogMapper blogMapper = session.getMapper(BlogMapper.class);

		List<Blog> bloglist = blogMapper.selectAllBlog();

		System.out.println(bloglist);
	}

	@Test
	public void testUpdateBlog() {
		
		//update 方式 一
		BlogMapper blogMapper1 = session.getMapper(BlogMapper.class);
		Blog blog1 = blogMapper1.findBlogById(11);
		BlogUser user1 = new BlogUser();
		user1.setId(11);
		blog1.setBlogUser(user1);
		blog1.setName("blog11");
		blog1.setImage("image11");
		blog1.setInfomation("info11");
		int row = blogMapper1.updateBlog(blog1);
		System.out.println("執行更新的條數 "+row);
		
		System.out.println(blog1.getId());
		
		//update 方式 二
		BlogMapper blogMapper2 = session.getMapper(BlogMapper.class);
		BlogUser user2 = new BlogUser();
		user2.setId(12);
		Blog blog2 = new Blog();
		blog2.setId(12);
		blog2.setBlogUser(user2);
		blog2.setName("blog12");
		blog2.setImage("image12");
		blog2.setInfomation("info12");
		blogMapper2.updateBlog(blog2);
		System.out.println(blog2.getId());
	}

	@Test
	public void testSaveBlog() {
		BlogMapper blogMapper = session.getMapper(BlogMapper.class);
		BlogUser user = new BlogUser();
		user.setId(6);
		Blog blog = new Blog();
		blog.setBlogUser(user);
		blog.setName("blog6");
		blog.setImage("image6");
		blog.setInfomation("info6");
		int row = blogMapper.saveBlog(blog);
		System.out.println("執行保存的條數 "+row);
		System.out.println(blog.getId());
	}
	
	@Test
	public void testDeleteBlog() {
		BlogMapper blogMapper = session.getMapper(BlogMapper.class);
		Blog blog = new Blog();
		blog.setId(14);
		int row = blogMapper.deleteBlog(blog);
		System.out.println("執行刪除的條數 "+row);
		System.out.println(blog.getId());
	}
}
